﻿# coding: utf-8
import wx
from PyDevIL import *

class wxImageList:
    ID_IMAGE_FILE_NEW = wx.NewId()
    ID_IMAGE_EXIT = wx.NewId()
    ID_IMAGE_SETTING = wx.NewId()
    ID_IMAGE_MODEL = wx.NewId()
    ID_IMAGE_BROWSE = wx.NewId()
    ID_IMAGE_MESH = wx.NewId()
    ID_IMAGE_ANIMATION = wx.NewId()
    ID_IMAGE_PYTHON_SHELL = wx.NewId()
    ID_IMAGE_GS_GENERAL = wx.NewId()
    ID_IMAGE_FOLDER_OPEN = wx.NewId()
    ID_IMAGE_FOLDER_CLOSE = wx.NewId()
    ID_IMAGE_FILE_FOLDER_OPEN = wx.NewId()
    ID_IMAGE_FILE_FOLDER_CLOSE = wx.NewId()
    ID_IMAGE_FILE_NORMAL = wx.NewId()
    ID_IMAGE_FILE_SELECTED = wx.NewId()
    ID_IMAGE_PLAY = wx.NewId()
    ID_IMAGE_PAUSE = wx.NewId()
    ID_IMAGE_STOP = wx.NewId()
    
    IMAGE_BIG = 32
    IMAGE_SMALL = 16
            
    def __init__(self):             
        self._bitmapList = {}
        
        # Initialize DevIL
        ilInit()
        
    def __LoadBitmap(self, id, path):
		try:
			bmpBig = wx.Image(path).Rescale(wxImageList.IMAGE_BIG, wxImageList.IMAGE_BIG).ConvertToBitmap()
			bmpSmall = wx.Image(path).Rescale(wxImageList.IMAGE_SMALL, wxImageList.IMAGE_SMALL).ConvertToBitmap()
			self._bitmapList[id] = {wxImageList.IMAGE_BIG : bmpBig, wxImageList.IMAGE_SMALL : bmpSmall}
		except:
			path = 'Resource/dummy.png'
			bmpBig = wx.Image(path).Rescale(wxImageList.IMAGE_BIG, wxImageList.IMAGE_BIG).ConvertToBitmap()
			bmpSmall = wx.Image(path).Rescale(wxImageList.IMAGE_SMALL, wxImageList.IMAGE_SMALL).ConvertToBitmap()
			self._bitmapList[id] = {wxImageList.IMAGE_BIG : bmpBig, wxImageList.IMAGE_SMALL : bmpSmall}

			         
    def LoadAllImages(self):
        self.__LoadBitmap(wxImageList.ID_IMAGE_FILE_NEW, 'Resource/icon/32px-Crystal_Clear_action_filenew.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_SETTING, 'Resource/icon/32px-Crystal_Clear_app_package_settings.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_EXIT, 'Resource/icon/32px-Crystal_Clear_action_exit.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_MODEL, 'Resource/icon/32px-Crystal_Clear_app_xeyes.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_BROWSE, 'Resource/icon/32px-Crystal_Clear_app_browser.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_MESH, 'Resource/icon/32px-Crystal_Clear_app_3d.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_ANIMATION, 'Resource/icon/32px-Crystal_Clear_mimetype_video.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_PYTHON_SHELL, 'Resource/icon/32px-Crystal_Clear_app_terminal.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_GS_GENERAL, 'Resource/icon/32px-Crystal_Clear_app_kfm_home.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_FOLDER_OPEN, 'Resource/icon/32px-Crystal_Clear_action_1uparrow.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_FOLDER_CLOSE, 'Resource/icon/32px-Crystal_Clear_action_1downarrow.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_FILE_FOLDER_OPEN, 'Resource/icon/32px-Crystal_Clear_filesystem_folder_grey_open.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_FILE_FOLDER_CLOSE, 'Resource/icon/32px-Crystal_Clear_filesystem_folder_grey.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_FILE_NORMAL, 'Resource/icon/32px-Crystal_Clear_mimetype_document.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_FILE_SELECTED, 'Resource/icon/32px-Crystal_Clear_action_apply.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_PLAY, 'Resource/icon32/CircleBlue/Play1Normal.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_PAUSE, 'Resource/icon32/PlainGreen/PausePressed.png')
        self.__LoadBitmap(wxImageList.ID_IMAGE_STOP, 'Resource/icon/32px-Crystal_Clear_action_apply.png')
        
    def GetBitmap(self, id, size):
        bmp = self._bitmapList.get(id, None)
        if bmp:
            return self._bitmapList[id][size]
        return None
        
    def LoadBitmap(self, path, size = None):
        """ use DevIL to load a bitmap """
        
        image = ilGenImage()
        ilBindImage(image)
        ilLoadImage(path)
        depth = ilGetInteger(IL_IMAGE_DEPTH)
        bpp = ilGetInteger(IL_IMAGE_BPP)
        print depth, bpp
        if size != None:
            width, height = size
            iluScale(size[0], size[1], depth)
        else:
            width = ilGetInteger(IL_IMAGE_WIDTH)
            height = ilGetInteger(IL_IMAGE_HEIGHT)
        bmp = wx.EmptyBitmap(width, height, bpp * 8)
        pixelData = wx.AlphaPixelData(bmp)
        data = ilGetData()
        if bpp == 3:
            for pixel in pixelData:
                pixel.Set(data[i], data[i+1], data[i+2])
                i+=3
        elif bpp == 4:
            for pixel in pixelData:
                pixel.Set(data[i], data[i+1], data[i+2], data[i+3])
                i+=4
        else:
            bmp = None      
        ilDeleteImage(image)
        return bmp
                
__builtins__['IL'] = wxImageList()